//
//  HCNavAnimationView.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/29.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCNavAnimationView.h"

@interface HCNavAnimationView () {
    UIView *promptView;
    UIButton *btn_one;
    UIButton *btn_two;
}

@end

@implementation HCNavAnimationView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViewsCustom:frame];
    }
    return self;
}

-(void)initSubViewsCustom:(CGRect)frame {
    btn_one = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2.0, frame.size.height)];
    [btn_one setTitleColor:YBKRGBCOLOR(255, 112, 132, 1) forState:UIControlStateSelected];
    [btn_one setTitle:@"婴儿区" forState:UIControlStateNormal];
    [btn_one setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btn_one.titleLabel.font = YBKSystemFont(YBKAdaptiveFont(15));
    [self addSubview:btn_one];
    btn_one.selected = YES;
    [btn_one addTarget:self action:@selector(btn_oneAction:) forControlEvents:UIControlEventTouchUpInside];
    
    btn_two = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width/2.0, 0, frame.size.width/2.0, frame.size.height)];
    [btn_two setTitleColor:YBKRGBCOLOR(255, 112, 132, 1) forState:UIControlStateSelected];
    [btn_two setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn_two setTitle:@"幼儿区" forState:UIControlStateNormal];
    btn_two.titleLabel.font = YBKSystemFont(YBKAdaptiveFont(15));
    [self addSubview:btn_two];
    btn_two.selected = NO;
    [btn_two addTarget:self action:@selector(btn_twoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    promptView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 2, frame.size.width/2.0, 2)];
    promptView.backgroundColor = YBKRGBCOLOR(255, 112, 132, 1);
    [self addSubview:promptView];
    
}

-(void)btn_oneAction:(UIButton *)btn {
    [self animateToLeft];
}

-(void)btn_twoAction:(UIButton *)btn {
    [self animateToRight];
}

-(void)animateToRight {
    btn_one.selected = !btn_one.selected;
    btn_two.selected = !btn_two.selected;
    
    [UIView animateWithDuration:.4f animations:^{
        promptView.frame = CGRectMake(self.frame.size.width/2.0, promptView.frame.origin.y, 0, 2);
    } completion:^(BOOL finished) {
       [UIView animateWithDuration:.4f animations:^{
           promptView.frame = CGRectMake(promptView.frame.origin.x, promptView.frame.origin.y, self.frame.size.width/2.0, 2);
       }];
    }];
    
}

-(void)animateToLeft {
    btn_one.selected = !btn_one.selected;
    btn_two.selected = !btn_two.selected;
    
    [UIView animateWithDuration:.4f animations:^{
        promptView.frame = CGRectMake(promptView.frame.origin.x, promptView.frame.origin.y, 0, 2);
    } completion:^(BOOL finished) {
       [UIView animateWithDuration:.4f animations:^{
           promptView.frame = CGRectMake(0, promptView.frame.origin.y, self.frame.size.width/2.0, 2);
       }];
    }];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
